package abel.wlmr.model.type;

import abel.util.StdBcdNumber;
import abel.wlmr.model.WlmrUserData;
import abel.wlmr.model.WlmrException;
import io.netty.buffer.ByteBuf;

/**
 * 表B.4 地址域格式
 * 地址域 数据格式 字节数
 * 行政区划码 A1 BCD 2
 * 终端地址 A2 BIN 2
 * 主站地址和组地址标志 A3 BIN 1
 * 
 * @author Abel
 *
 */
public class WlmrAddrBody extends WlmrUserData {

    private static final short FIXED_LEN = 2 + 2 + 1;

    /**
     * 行政区划码 A1 BCD 2
     */
    private StdBcdNumber a1;

    /**
     * 终端地址 A2 BIN 2
     */
    private int a2;

    /**
     * 主站地址和组地址标志 A3 BIN 1
     */
    private GroupFlagAddrField a3 = new GroupFlagAddrField();

    public WlmrAddrBody() {
        super(FIXED_LEN);
    }

    public ByteBuf appendBuf(ByteBuf buf) {
        if (a1.length() != 2) {
            throw new IllegalArgumentException("A1 should be of length 2.");
        }
        buf.writeBytes(a1.toBytes());

        buf.writeShortLE(a2);

        buf.writeByte(a3.getValue());

        return buf;
    }

    public int parseBuf(ByteBuf buf) throws WlmrException {
        if (buf.readableBytes() < getGuessedLength()) {
            return -1;
        }

        a1 = new StdBcdNumber();
        a1.populateBytes(buf, 2);

        a2 = buf.readUnsignedShortLE();

        a3 = new GroupFlagAddrField();
        a3.parseValue(buf.readByte());

        return 5;
    }

    /* (non-Javadoc)
     * @see abel.wlmr.model.WlmrDataPart#getFinalLen()
     */
    @Override
    public short getFinalLen() {
        return FIXED_LEN;
    }

    /* (non-Javadoc)
     * @see abel.wlmr.model.WlmrDataPart#getMinLength()
     */
    @Override
    public int getMinLength() {
        return FIXED_LEN;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("{a1:%s, a2:%s, a3:%s}", a1, a2, a3);
    }

    /**
     * This key is used for identifying station.
     * 
     * @return
     */
    public String stationKey() {
        return String.format("%s.%s", a1.getAsc(), a2);
    }

    /**
     * 行政区划码 A1 BCD 2
     * 
     * @return the a1
     */
    public StdBcdNumber getA1() {
        return a1;
    }

    /**
     * 行政区划码 A1 BCD 2
     * 
     * @param a1
     *            the a1 to set
     */
    public void setA1(StdBcdNumber a1) {
        this.a1 = a1;
    }

    /**
     * 终端地址 A2 BIN 2
     * 
     * @return the a2
     */
    public int getA2() {
        return a2;
    }

    /**
     * 终端地址 A2 BIN 2
     * 
     * @param a2
     *            the a2 to set
     */
    public void setA2(int a2) {
        this.a2 = a2;
    }

    /**
     * 主站地址和组地址标志 A3 BIN 1
     * 
     * @return the a3
     */
    public GroupFlagAddrField getA3() {
        return a3;
    }

    /**
     * 主站地址和组地址标志 A3 BIN 1
     * 
     * @param a3
     *            the a3 to set
     */
    public void setA3(GroupFlagAddrField a3) {
        this.a3 = a3;
    }

}
